﻿using System;
using Bingosoft.Web.Utils;
using Bingosoft.Data;

namespace Bingosoft.WebApp.Modules.DataDictionary
{
    public partial class DataDictionaryItemEdit : System.Web.UI.FormPage
    {
        private Dao dao = Dao.Get();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (SmartForm.IsInsert())
                {
                    var dictionaryId = Request.Params["DictionaryId"];
                    this.DictionaryId.Text = dictionaryId;
                    this.Status.Text = "enabled";
                }
                else
                {
                    SmartForm.Fill(this.container, "dataDictionaryItem.select");
                }
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            var data = SmartForm.GetDictionary(this.container);
            int count = dao.QueryScalar<int>("dataDictionaryItem.checkItemCodeUnique", data);
            if (count > 0)
            {
                ScriptHelper.Alert(this, "该数据字典项已存在，请修改编码确保在当前数据字典中唯一！");
                return;
            }
            if (string.IsNullOrEmpty(data["Id"] + ""))
            {
                dao.ExecuteNonQuery("dataDictionaryItem.insert", data);
            }
            else
            {
                dao.ExecuteNonQuery("dataDictionaryItem.update", data);
            }
            ScriptHelper.CloseDialog(this, "true");
        }
    }
}